package com.csxy.six.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.csxy.six.entity.Role;
import com.csxy.six.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {

    @Select("SELECT r.* FROM role r " +
            "JOIN user_role ur ON r.id = ur.role_id " +
            "WHERE ur.user_id = #{userId}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "description", column = "description"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")
    })
    List<Role> findRolesByUserId(Long userId);

    @Select("SELECT * FROM user WHERE username = #{username}")
    User findByUsername(String username);

    @Insert("INSERT INTO user_role (user_id, role_id, create_time) " +
            "VALUES (#{userId}, #{roleId}, NOW())")
    void addRoleToUser(@Param("userId") Long userId, @Param("roleId") Long roleId);

    // 添加新方法：根据角色名称查询角色
    @Select("SELECT * FROM role WHERE name = #{name}")
    Role findRoleByName(String name);
}